local qinguo = fk.CreateSkill {

  name = "joy__qinguo",

  tags = {  },

}



qinguo:addEffect(fk.CardUseFinished, {
  name = "joy__qinguo",
  mute = true,
  events = {fk.CardUseFinished, fk.AfterCardsMove,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qinguo.name) and player.phase ~= Player.NotActive then
      if event == fk.CardUseFinished then
        return target == player and data.card.type == Card.TypeEquip
      elseif event == fk.AfterCardsMove then
        local equipnum = #player:getCardIds("e")
        for _, move in ipairs(data) do
          for _, info in ipairs(move.moveInfo) do
            if move.from==player and info.fromArea == Card.PlayerEquip then
              equipnum = equipnum + 1
            elseif move.to==player and move.toArea == Card.PlayerEquip then
              equipnum = equipnum - 1
            end
          end
        end
        return #player:getCardIds("e") ~= equipnum and #player:getCardIds("e") >= player.hp and player:isWounded()
      elseif event == fk.Damage then
        return target == player and player:hasSkill(qinguo.name) and data.card and data.card.skillName and table.contains(data.card.skillNames,qinguo.name)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      local room = player.room
      local success, dat = room:askForUseActiveSkill(player, "joy__qinguo_viewas", "#joy__qinguo-ask", true)
      if success then
        event:setCostData(self, dat)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(qinguo.name)
    if event == fk.CardUseFinished then
      room:notifySkillInvoked(player, qinguo.name, "offensive")
      local card = Fk.skills["joy__qinguo_viewas"]:viewAs(event:getCostData(self).cards)
      room:useCard{
        from = player,
        tos = table.map(event:getCostData(self).targets, function(id) return room:getPlayerById(id) end),
        card = card,
        extraUse = true,
      }
    elseif event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, qinguo.name, "support")
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = qinguo.name,
      }
    elseif event == fk.Damage then
      player:drawCards(1,qinguo.name)
    end
  end,
})
qinguo:addEffect(fk.AfterCardsMove, {
  name = "joy__qinguo",
  mute = true,
  events = {fk.CardUseFinished, fk.AfterCardsMove,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qinguo.name) and player.phase ~= Player.NotActive then
      if event == fk.CardUseFinished then
        return target == player and data.card.type == Card.TypeEquip
      elseif event == fk.AfterCardsMove then
        local equipnum = #player:getCardIds("e")
        for _, move in ipairs(data) do
          for _, info in ipairs(move.moveInfo) do
            if move.from==player and info.fromArea == Card.PlayerEquip then
              equipnum = equipnum + 1
            elseif move.to==player and move.toArea == Card.PlayerEquip then
              equipnum = equipnum - 1
            end
          end
        end
        return #player:getCardIds("e") ~= equipnum and #player:getCardIds("e") >= player.hp and player:isWounded()
      elseif event == fk.Damage then
        return target == player and player:hasSkill(qinguo.name) and data.card and data.card.skillName and table.contains(data.card.skillNames,qinguo.name)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      local room = player.room
      local success, dat = room:askForUseActiveSkill(player, "joy__qinguo_viewas", "#joy__qinguo-ask", true)
      if success then
        event:setCostData(self, dat)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(qinguo.name)
    if event == fk.CardUseFinished then
      room:notifySkillInvoked(player, qinguo.name, "offensive")
      local card = Fk.skills["joy__qinguo_viewas"]:viewAs(event:getCostData(self).cards)
      room:useCard{
        from = player.id,
        tos = table.map(event:getCostData(self).targets, function(id) return {id} end),
        card = card,
        extraUse = true,
      }
    elseif event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, qinguo.name, "support")
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = qinguo.name,
      }
    elseif event == fk.Damage then
      player:drawCards(1,qinguo.name)
    end
  end,
})
qinguo:addEffect(fk.Damage, {
  name = "joy__qinguo",
  mute = true,
  events = {fk.CardUseFinished, fk.AfterCardsMove,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qinguo.name) and player.phase ~= Player.NotActive then
      if event == fk.CardUseFinished then
        return target == player and data.card.type == Card.TypeEquip
      elseif event == fk.AfterCardsMove then
        local equipnum = #player:getCardIds("e")
        for _, move in ipairs(data) do
          for _, info in ipairs(move.moveInfo) do
            if move.from==player and info.fromArea == Card.PlayerEquip then
              equipnum = equipnum + 1
            elseif move.to==player and move.toArea == Card.PlayerEquip then
              equipnum = equipnum - 1
            end
          end
        end
        return #player:getCardIds("e") ~= equipnum and #player:getCardIds("e") >= player.hp and player:isWounded()
      elseif event == fk.Damage then
        return target == player and player:hasSkill(qinguo.name) and data.card and data.card.skillName and table.contains(data.card.skillNames,qinguo.name)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      local room = player.room
      local success, dat = room:askForUseActiveSkill(player, "joy__qinguo_viewas", "#joy__qinguo-ask", true)
      if success then
        event:setCostData(self, dat)
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(qinguo.name)
    if event == fk.CardUseFinished then
      room:notifySkillInvoked(player, qinguo.name, "offensive")
      local card = Fk.skills["joy__qinguo_viewas"]:viewAs(event:getCostData(self).cards)
      room:useCard{
        from = player.id,
        tos = table.map(event:getCostData(self).targets, function(id) return {id} end),
        card = card,
        extraUse = true,
      }
    elseif event == fk.AfterCardsMove then
      room:notifySkillInvoked(player, qinguo.name, "support")
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = qinguo.name,
      }
    elseif event == fk.Damage then
      player:drawCards(1,qinguo.name)
    end
  end,
})

qinguo:addEffect("targetmod", {
  name = "#joy__qinguo_targetmod",
  bypass_times = function(self, player, skill, scope, card)
    return card and table.contains(card.skillNames, "joy__qinguo")
  end,
})

return qinguo